<?php


namespace app\api\controller;

use app\admin\model\User;
use app\admin\model\ActiveCases;
use app\admin\model\ListContent;
use app\admin\model\Recorded;
use app\admin\model\LargerActive;

use think\Request;

class ShopCart extends Base
{
    public function addCart(Request $request){
        $res = [];
        $data = $request->post();
        //user_id  pr_id  pr_type  pr_img pr_name
        if(!isset($data['user_id']) or !isset($data['pr_id']) or !isset($data['pr_type']) or !isset($data['pr_img']) or !isset($data['pr_name'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $user = new User();
        $userWhere['id'] = $data['user_id'];
        $userInfo = $user->getOne($userWhere);
        if(!$userInfo){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '用户不存在';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        if($data['pr_type'] == 0){
            //场地
            $db = new ListContent();
        }else if($data['pr_type'] == 1){
            //预知现场
            $db = new ActiveCases();
        }else if($data['pr_type'] == 2){
            //录制活动
            $db = new Recorded();
        }else if($data['pr_type'] == 3){
            //大型活动
            $db = new LargerActive();
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = 'type参数错误';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        $where['id'] = $data['pr_id'];
        $info = $db->getOne($where);

        if($info){
            $shopCart = new \app\admin\model\ShopCart();

            $cartWhere['user_id'] = $data['user_id'];
            $cartWhere['pr_id'] = $data['pr_id'];
            $cartWhere['pr_type'] = $data['pr_type'];
            $inCart = $shopCart->getOne($cartWhere);
            if($inCart){
                $res['data']['code'] = 0;
                $res['data']['msg'] = '请勿重复添加购物车';
                $res['status'] = 200;
                echo json_encode($res);die;
            }

            $addData['user_id'] = $data['user_id'];
            $addData['pr_id'] = $data['pr_id'];
            $addData['pr_type'] = $data['pr_type'];
            $addData['pr_name'] = $data['pr_name'];
            $addData['pr_img'] = $data['pr_img'];

            $rs =  $shopCart->add($addData);
            if($rs){
                $res['data']['code'] = 1;
                $res['data']['msg'] = '添加购物车成功';
            }else{
                $res['data']['code'] = 0;
                $res['data']['msg'] = '添加购物车失败';
            }
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '产品信息不存在';
        }

        $res['status'] = 200;
        echo json_encode($res);die;
    }

    public function getCart(Request $request){
        $res = [];
        $data = $request->post();

        if(!isset($data['user_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $user = new User();
        $userWhere['id'] = $data['user_id'];
        $userInfo = $user->getOne($userWhere);
        if(!$userInfo){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '用户不存在';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        //获取活动
        $shopCart = new \app\admin\model\ShopCart();
        $where['user_id'] = $data['user_id'];
        $where['pr_type'] = ['neq',0];
        $activeList = $shopCart->getAll($where);

        //获取场地
        $where['pr_type'] = ['eq',0];
        $selectedList = $shopCart->getAll($where);

        if(count($activeList)<1 and count($selectedList)<1 ){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '空空如也';
        }else{
            $res['data']['code'] = 1;
            $res['data']['msg'] = '查询成功';
            $res['data']['list_active'] = $activeList;
            $res['data']['list_selected'] = $selectedList;
        }

        $res['status'] = 200;
        echo json_encode($res);die;
    }

    public function delCart(Request $request){
        $res = [];
        $data = $request->post();
        if(!isset($data['user_id']) or !isset($data['pr_type']) or !isset($data['pr_id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $user = new User();
        $userWhere['id'] = $data['user_id'];
        $userInfo = $user->getOne($userWhere);
        if(!$userInfo){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '用户不存在';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        $shopCart = new \app\admin\model\ShopCart();

        $cartWhere['user_id'] = $data['user_id'];
        $cartWhere['pr_id'] = $data['pr_id'];
        $cartWhere['pr_type'] = $data['pr_type'];
        $inCart = $shopCart->getOne($cartWhere);
        if(!$inCart){
            $res['data']['code'] = 0;
            $res['data']['msg'] = '产品不存在';
            $res['status'] = 200;
            echo json_encode($res);die;
        }

        $delWhere['id'] = $inCart['id'];
        $rs = $shopCart->del($delWhere);

        if($rs){
            $res['data']['code'] = 1;
            $res['data']['msg'] = '删除成功';
        }else{
            $res['data']['code'] = 0;
            $res['data']['msg'] = '删除失败';
        }

        $res['status'] = 200;
        echo json_encode($res);die;
    }

}